package com.alex.maze.graphics;

import com.alex.maze.Game;
import com.alex.maze.level.Level;
import com.alex.maze.level.block.Block;
import java.util.Random;

public class Screen extends Bitmap
{
    private static final int VISINAPANELA = 8 * 3;
    private Bitmap testBitmapa; // Najverovatnije ima da bude meni
    private Bitmap igrackiPanel; // Ili ovo ima da bude panel... hmm.
    private Bitmap3D viewport; // Stvarno nemam predstave kako da ovo prevedem.
    
    public Screen(int sirina, int visina)
    {
        super(sirina, visina);
        
        /*
            igracki panel i viewport(pogled iz prvog lica) sa custom velicinama (nije vise 64x64).
        */
        igrackiPanel = new Bitmap(sirina, VISINAPANELA);
        viewport = new Bitmap3D(sirina, visina - VISINAPANELA);
        
        /*  Koristimo random broj i taj random broj ima da predstavja boju pojedinacnog piksela! */
        Random random = new Random();
        /*  
            Test bitmapa velicine 64 piksela sa 64 piksela!
            Pojedinacan piksel ima da izgleda 4 puta vece zbog vrednosti int-a SKALA!
        */
        testBitmapa = new Bitmap(64, 64);
        /*  
            for petlja koja daje vrednost(boju) pojedinacnom pikselu!
            Da naprimer svaki piksel bude plav, promenite:
            testBitmapa.pikseli[i] = 0x0000FF;
        */
        for (int i = 0; i < 64 * 64; i++)
        {
            testBitmapa.pikseli[i] = random.nextInt() * (random.nextInt(5) / 4); // Bitmasking... da objasnim kasnije...
        }
    }

    /*  kao sto ime kaze renderuje! */
    public void render(Game igra, boolean hasFocus)
    {
        /*
            Ova for petlja cisti ekran!
            To znaci da svaku vrednost piksela postavlja na 0.
            Pa onda iscrtavamo ekran!
            To znaci da tek onda dodeljujemo vrenost piksela.
        */
        //for(int i = 0; i < BSIRINA * BVISINA; i++)
        //{
        //    pikseli[i] = 0;
        //}
        
        //for (int i = 0; i < 48; i++)
        //{
        //    int xo = (igra.vreme + i * 8) % 400 - 200;
        //    int yo = 0;
            //igrackiPanel.draw(testBitmapa, (igrackiPanel.BSIRINA - 64) / 2 + xo, (igrackiPanel.BVISINA - 64) / 2 + yo);
        //}
        
        /*  Iscrtavanje grafike */
        viewport.render(igra);
        viewport.postProcess();
        
        Block block = igra.nivo.getBlock((int) (igra.igrac.x + 0.5), (int) (igra.igrac.z + 0.5));
        
        if (block.messages != null && hasFocus)
        {
            for(int i = 0; i < block.messages.length; i++) // Baj teh vej ovo ubija neki 50 - 100fps... al kada se koriste samo 2ili3 onda nije problem! xD
            {
                viewport.draw(block.messages[i], (BSIRINA - block.messages[i].length() * 6) / 2, (viewport.BVISINA - block.messages.length * 48) / 2 + i * 16 + 1, 0x111111);
                viewport.draw(block.messages[i], (BSIRINA - block.messages[i].length() * 6) / 2, (viewport.BVISINA - block.messages.length * 48) / 2 + i * 16, 0x555544);
                //viewport.draw(block.messages[i], (BSIRINA - block.messages[i].length() * 6) / 2 - 1, (viewport.BVISINA - block.messages.length * 48) / 2 + i * 16, 0); //20 + i * 15
                //viewport.draw(block.messages[i], (BSIRINA - block.messages[i].length() * 6) / 2 + 1, (viewport.BVISINA - block.messages.length * 48) / 2 + i * 16, 0);
                //viewport.draw(block.messages[i], (BSIRINA - block.messages[i].length() * 6) / 2, (viewport.BVISINA - block.messages.length * 48) / 2 + i * 16 - 1, 0);
                //viewport.draw(block.messages[i], (BSIRINA - block.messages[i].length() * 6) / 2, (viewport.BVISINA - block.messages.length * 48) / 2 + i * 16 + 1, 0);
                //viewport.draw(block.messages[i], (BSIRINA - block.messages[i].length() * 6) / 2 + 1, (viewport.BVISINA - block.messages.length * 48) / 2 + i * 16 + 1, 0);
                //viewport.draw(block.messages[i], (BSIRINA - block.messages[i].length() * 6) / 2 + 1, (viewport.BVISINA - block.messages.length * 48) / 2 + i * 16 + 2, 0);
                //viewport.draw(block.messages[i], (BSIRINA - block.messages[i].length() * 6) / 2, (viewport.BVISINA - block.messages.length * 48) / 2 + i * 16, 0x555555);
            }
        }

        //viewport.draw(block.message, 10, 10);
        //igrackiPanel.draw("Zdravo! 1337 @", 10, 10);
        
        //flipDraw(viewport, 0, 0);
        draw(viewport, 0, 0);
        draw(igrackiPanel, 0, BVISINA - VISINAPANELA);
        
        if (!hasFocus)  
        {
            for (int i = 0; i < pikseli.length; i++)
            {
                pikseli[i] = (pikseli[i] & 0xFEFEFE) >> 1;
            }
            if (System.currentTimeMillis() / 400 % 2 != 0) // BLINK BLINK!!!! xDDD
            {
                String poruka = "Click to focus!";
                //viewport.draw(poruka, (BSIRINA - poruka.length() * 6) / 2 - 1, 10, 0x000000);
                //viewport.draw(poruka, (BSIRINA - poruka.length() * 6) / 2 + 1, 10, 0x000000);
                draw(poruka, (BSIRINA - poruka.length() * 6) / 2, BVISINA / 3 + 4, 0xFFFFFF);
            }
            //String poruka = "Click to focus!";
            //viewport.draw(poruka, (BSIRINA - poruka.length() * 6) / 2, 10, 0xFFFFFF);
        }
    }
}
